<?php
@session_start();
@error_reporting(0);
@ini_set('display_errors', 'Off');

header('Content-Type: image/png');
header('Cache-Control: no-cache, must-revalidate');

$width = 120;
$height = 48;
$image = imagecreatetruecolor($width, $height);

if (!function_exists('imagepng')) {
    $bg_color = imagecolorallocate($image, 255, 230, 230);
    $text_color = imagecolorallocate($image, 200, 0, 0);
    imagefill($image, 0, 0, $bg_color);
    imagestring($image, 2, 5, 15, 'GD lib not found', $text_color);
    imagepng($image);
    imagedestroy($image);
    exit;
}

$bg_color = imagecolorallocate($image, 248, 249, 250);
$text_colors = [
    imagecolorallocate($image, 55, 65, 81), imagecolorallocate($image, 74, 105, 189),
    imagecolorallocate($image, 59, 130, 246), imagecolorallocate($image, 40, 40, 40)
];
$line_color = imagecolorallocate($image, 222, 226, 230);
imagefill($image, 0, 0, $bg_color);

$chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$code = '';
for ($i = 0; $i < 4; $i++) {
    $code .= $chars[mt_rand(0, strlen($chars) - 1)];
}
$_SESSION['captcha_code'] = strtolower($code);

for ($i = 0; $i < 5; $i++) {
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $line_color);
}

$font_map = [
    'A' => [0x18,0x24,0x42,0x42,0x7E,0x42,0x42], 'B' => [0x7C,0x42,0x42,0x7C,0x42,0x42,0x7C], 'C' => [0x3C,0x42,0x40,0x40,0x40,0x42,0x3C], 'D' => [0x78,0x44,0x42,0x42,0x42,0x44,0x78],
    'E' => [0x7E,0x40,0x40,0x7C,0x40,0x40,0x7E], 'F' => [0x7E,0x40,0x40,0x7C,0x40,0x40,0x40], 'G' => [0x3C,0x42,0x40,0x4E,0x42,0x42,0x3E], 'H' => [0x42,0x42,0x42,0x7E,0x42,0x42,0x42],
    'J' => [0x0C,0x04,0x04,0x04,0x04,0x44,0x38], 'K' => [0x44,0x48,0x50,0x60,0x50,0x48,0x44], 'L' => [0x40,0x40,0x40,0x40,0x40,0x40,0x7E], 'M' => [0x42,0x66,0x5A,0x5A,0x42,0x42,0x42],
    'N' => [0x42,0x62,0x52,0x4A,0x46,0x42,0x42], 'P' => [0x7C,0x42,0x42,0x7C,0x40,0x40,0x40], 'Q' => [0x3C,0x42,0x42,0x42,0x52,0x4A,0x3A], 'R' => [0x7C,0x42,0x42,0x7C,0x50,0x48,0x44],
    'S' => [0x3E,0x40,0x40,0x3C,0x02,0x02,0x7C], 'T' => [0x7E,0x18,0x18,0x18,0x18,0x18,0x18], 'U' => [0x42,0x42,0x42,0x42,0x42,0x42,0x3C], 'V' => [0x42,0x42,0x42,0x42,0x42,0x24,0x18],
    'W' => [0x42,0x42,0x42,0x5A,0x5A,0x66,0x42], 'X' => [0x42,0x42,0x24,0x18,0x24,0x42,0x42], 'Y' => [0x42,0x42,0x42,0x24,0x18,0x18,0x18], 'Z' => [0x7E,0x04,0x08,0x10,0x20,0x40,0x7E],
    '2' => [0x3C,0x42,0x02,0x0C,0x30,0x40,0x7E], '3' => [0x3C,0x02,0x02,0x3C,0x02,0x02,0x3C], '4' => [0x08,0x18,0x28,0x48,0x7E,0x08,0x08], '5' => [0x7E,0x40,0x40,0x7C,0x02,0x02,0x7C],
    '6' => [0x3C,0x40,0x40,0x7C,0x42,0x42,0x3C], '7' => [0x7E,0x02,0x04,0x08,0x10,0x10,0x10], '8' => [0x3C,0x42,0x42,0x3C,0x42,0x42,0x3C], '9' => [0x3C,0x42,0x42,0x3E,0x02,0x04,0x38]
];

function draw_char($image, $char, $x, $y, $color, $size) {
    global $font_map;
    $char_map = $font_map[strtoupper($char)] ?? [];
    for ($j = 0; $j < 8; $j++) { 
        for ($k = 0; $k < 8; $k++) { 
            if (isset($char_map[$j]) && ($char_map[$j] >> (7 - $k)) & 1) {
                imagefilledrectangle($image, $x + $k * $size, $y + $j * $size, $x + $k * $size + $size -1, $y + $j * $size + $size -1, $color);
            }
        }
    }
}

$char_x = 15;
for ($i = 0; $i < strlen($code); $i++) {
    $color = $text_colors[array_rand($text_colors)];
    draw_char($image, $code[$i], $char_x + mt_rand(-2, 2), 8 + mt_rand(-3, 3), $color, 3);
    $char_x += 25;
}

for ($i = 0; $i < 100; $i++) {
    imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $line_color);
}

imagepng($image);
imagedestroy($image);
exit;
?>